package cn.yuanqiao.manage.service.impl;

import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.manage.domain.ManageSys;
import cn.yuanqiao.manage.domain.ManageTenant;
import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.manage.mapper.ManageSysMapper;
import cn.yuanqiao.manage.mapper.ManageTenantMapper;
import cn.yuanqiao.manage.service.IManageSysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 租户三员Service业务层处理
 * 
 * @author yuanqiao
 * @date 2023-06-09
 */
@Service
public class ManageSysServiceImpl implements IManageSysService
{
    @Autowired
    private ManageSysMapper manageSysMapper;
    @Autowired
    private ManageTenantMapper manageTenantMapper;
    @Value("${encryption}")
    private String encryption;

    /**
     * 查询租户三员
     * 
     * @param ID 租户三员主键
     * @return 租户三员
     */
    @Override
    public ManageSys selectManageSysByID(String ID)
    {
        return manageSysMapper.selectManageSysByID(ID);
    }

    /**
     * 查询租户三员列表
     * 
     * @param manageSys 租户三员
     * @return 租户三员
     */
    @Override
    public List<ManageSys> selectManageSysList(ManageSys manageSys)
    {
        return manageSysMapper.selectManageSysList(manageSys);
    }

    /**
     * 新增租户三员
     * 
     * @param manageSys 租户三员
     * @return 结果
     */
    @Override
    public int insertManageSys(ManageSys manageSys)
    {
        manageSys.setCreateTime(DateUtils.getNowDate());
        return manageSysMapper.insertManageSys(manageSys);
    }

    /**
     * 修改租户三员
     * 
     * @param manageSys 租户三员
     * @return 结果
     */
    @Override
    public int updateManageSys(ManageSys manageSys)
    {
        manageSys.setUpdateTime(DateUtils.getNowDate());
        String ps = manageSysMapper.getPassWordById(manageSys.getId());
        if(!ps.equals(manageSys.getPassWord())) manageSys.setPassWord(SecurityUtils.encryptPassword(manageSys.getPassWord(),encryption));
        return manageSysMapper.updateManageSys(manageSys);
    }

    /**
     * 批量删除租户三员
     * 
     * @param IDs 需要删除的租户三员主键
     * @return 结果
     */
    @Override
    public int deleteManageSysByIDs(String[] IDs)
    {
        return manageSysMapper.deleteManageSysByIDs(IDs);
    }

    /**
     * 删除租户三员信息
     * 
     * @param ID 租户三员主键
     * @return 结果
     */
    @Override
    public int deleteManageSysByID(String ID)
    {
        return manageSysMapper.deleteManageSysByID(ID);
    }

    @Override
    public List<TreeSelect> selectTenantTreeList() {
        ManageTenant manageTenant = new ManageTenant();
        List<ManageTenant> manageTenants = manageTenantMapper.selectManageTenantList(manageTenant);
        TreeSelect treeSelect = new TreeSelect();
        treeSelect.setId(1L);
        treeSelect.setLabel("租户信息");
        ArrayList<TreeSelect> list = new ArrayList<>();
        Iterator<ManageTenant> iterator = manageTenants.iterator();
        while(iterator.hasNext()){
            ManageTenant mt = iterator.next();
            TreeSelect ts = new TreeSelect();
            ts.setId(mt.getId());
            ts.setTenantCode(mt.getTenantCode());
            ts.setLabel(mt.getRegisteredUnit());
            list.add(ts);
        }
        treeSelect.setChildren(list);
        ArrayList<TreeSelect> finalTreeSelect = new ArrayList<>();
        finalTreeSelect.add(treeSelect);
        return finalTreeSelect;
    }
}
